/*
对妈妈班进行拜访时，把拜访的坐标和时间同步到对应的callplan记录
create by xiaojiang 20110429
*/

trigger SynCoordinateConsumerEdu on ConsumerEducation__c (after update) {
    //查询妈妈班的记录类型 
    list<recordtype> lstRT=new list<recordtype>();
    id IdKNH;
    lstRT=[select id,DeveloperName from recordtype where sobjecttype=:'ConsumerEducation__c' and DeveloperName=:'p_class'];
    if(lstRT.size()>0){
        IdKNH=lstRT[0].id;
    }
    
    if(IdKNH!=null&&trigger.new.size()>0){
        set<id> sActivityId=new set<id>();
        map<id,ConsumerEducation__c> mapConsumer=new map<id,ConsumerEducation__c>();
        
        for(ConsumerEducation__c h:trigger.new){
             if(h.RecordTypeId==IdKNH&&h.lat__c!=null&&h.lon__c!=null&&h.StartTime__c!=null){
                 mapConsumer.put(h.id,h);
                 sActivityId.add(h.id);
             }   
        }
        
        //查找对应的callplan记录
        list<CallPlan__c> lCallPlan=new list<CallPlan__c>();
        if(sActivityId.size()>0){
           lCallPlan=[select id,StartTime__c,lat__c,lon__c,ActivityId__c from CallPlan__c where ActivityId__c in:sActivityId]; 
        }
        
        //更新对应的callplan记录
        ConsumerEducation__c objHCP=new ConsumerEducation__c();
        if(lCallPlan.size()>0){
            for(CallPlan__c cp:lCallPlan){
                objHCP=mapConsumer.get(cp.ActivityId__c);
                cp.StartTime__c=objHCP.StartTime__c;
                cp.lat__c=objHCP.lat__c;
                cp.lon__c=objHCP.lon__c;
            }
            
            update lCallPlan;
            
        }
        
    }
}